<?php
include dirname(dirname(dirname(__FILE__))).'/include/db_connect.php';
function dowith_sql($str)
{
	$str = str_replace("and","",$str);
	$str = str_replace("execute","",$str);
	$str = str_replace("update","",$str);
	$str = str_replace("count","",$str);
	$str = str_replace("chr","",$str);
	$str = str_replace("mid","",$str);
	$str = str_replace("master","",$str);
	$str = str_replace("truncate","",$str);
	$str = str_replace("char","",$str);
	$str = str_replace("declare","",$str);
	$str = str_replace("select","",$str);
	$str = str_replace("create","",$str);
	$str = str_replace("delete","",$str);
	$str = str_replace("insert","",$str);
	$str = str_replace("'","",$str);
	//$str = str_replace(""","",$str);
	$str = str_replace(" ","",$str);
	$str = str_replace("or","",$str);
	$str = str_replace("=","",$str);
	$str = str_replace("%20","",$str);
	//echo $str;
	return $str;
}

function alertInfo($msg,$url) {
	echo '<!DOCTYPE html><html><head><script>alert("'.$msg.'");
		window.location="'.$url.'";</script></head><body></body></html>';
}

function getPlaceApplyByUserId($s_user_id){

	$conn=get_db_conn();
	$sql='select pa.*,pat.table_content,pat.date
			 from place_apply pa left join place_apply_table
				pat on pa.place_apply_table_id=pat.id
				where pa.s_user_id="'.$s_user_id.'" order by pa.create_time desc;';
	$ret = mysql_query($sql,$conn);
	if ($ret) {
		mysql_close($conn);
		return $ret;
	}else {
		echo "error: ".mysql_error();
		mysql_close($conn);
		return false;
	}

}
/**
 * 
 * @param unknown $ret
 * @param unknown $date
 * @param unknown $period
 * use it after getPlaceApplyByUserId
 * status:0为待审核，1为通过审核，2为拒绝
 */
function isApplyOrNot($s_user_id,$date,$period){
	
	$ret= getPlaceApplyByUserId($s_user_id);
	while ($row=mysql_fetch_array($ret)){
		if ($row["date"]==$date) {
		
			if ($row["period"]==$period) {
				if ($row["status"]==0) {
					return 1;
				}
				if ($row["status"]==1) {
					return 2;
				}
				if ($row["status"]==2) {
					return 3;
				}

			}
		}
		
	}
	return 0;
	
}

function getPlaceApplyTable(){
	$conn=get_db_conn();
	$sql="select * from place_apply_table;";
	$ret = mysql_query($sql,$conn);
	if ($ret) {
		mysql_close($conn);
		return $ret;
	}else {
		echo "error: ".mysql_error();
		mysql_close($conn);
		return false;
	}
	
}

function getPlaceApplyTableByIndex($tableIndex){
	$conn=get_db_conn();
	$sql="select * from place_apply_table limit ".$tableIndex.",1;";
	$ret = mysql_query($sql,$conn);
	if ($ret) {
		mysql_close($conn);
		return $ret;
	}else {
		echo "error: ".mysql_error();
		mysql_close($conn);
		return false;
	}

}


function addApply($tableIndex,$period,$s_user_id){
	
	$ret=getPlaceApplyTableByIndex($tableIndex);
	$place_apply_id="";
	if ($ret) {
		$row=mysql_fetch_array($ret);
		$place_apply_table_id=$row["id"];
		$conn=get_db_conn();
		$sql="insert into place_apply(s_user_id,place_apply_table_id,period,create_time) values(".$s_user_id.
		",".$place_apply_table_id.",".$period.",".'"'.date("Y-m-d H:i:s").'"'.");";
		if(mysql_query($sql,$conn)){  return true;}
		else  {
			echo $sql;
			echo mysql_error();
			return false;
		}
	}else {
		return false;
	}
	
	
}

function addAcessory($storeName,$applyId,$s_username){
	$conn=get_db_conn();
	$sql="insert into accessory(name,apply_id,dir) values(".'"'.$storeName.'"'.",".$applyId.",".'"'.$s_username.'"'.");";
	if(mysql_query($sql,$conn))return true;
	else  {
		echo $sql;
		echo mysql_error();
		return false;
	}
	
}
?>